package com.hspedu.tankgame05;

import java.util.Vector;

@SuppressWarnings({"all"})
public class EnemyTank extends Tank implements Runnable{
    Shot shot = null;
    //创建敌人子弹，并使用Vector保存
    Vector<Shot> shots = new Vector<>();
    //增加一个成员，使EnemyTank可以获得敌人坦克的Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    boolean isALive = true;
    public EnemyTank(int x, int y) {
        super(x, y);
    }

    //提供一个方法，可以将MyPanel中的Vector<EnemyTank> enemyTanks = new Vector<>();
    //设置到EnemyTank 的成员 enemyTanks

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    //碰撞检测：查看当前坦克是否和其他坦克重叠
    public boolean isTouchEnemyTank() {
        //获取当前坦克方向
        switch (this.getDirect()) {
            case 0://上
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比较
                    if (enemyTank != this) {
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克左上角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右上角坐标 [this.getX() + 40,this.getY()]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //当前坦克左上角坐标
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右上角坐标 [this.getX() + 40,this.getY()]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1://右
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比较
                    if (enemyTank != this) {
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克右上角坐标 [this.getX() + 60,this.getY()]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 60,this.getY() + 40]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            ////1. 当前坦克右上角坐标 [this.getX() + 60,this.getY()]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 60,this.getY() + 40]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2://下
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比较
                    if (enemyTank != this) {
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克左下角坐标 [this.getX(),this.getY() + 60]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 40,this.getY() + 60]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            ////1. 当前坦克左下角坐标 [this.getX(),this.getY() + 60]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 40,this.getY() + 60]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3://左
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比较
                    if (enemyTank != this) {
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克左上角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            ////1. 当前坦克左上角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克左下角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }
    @Override
    public void run() {
        while (true) {
            //如果子弹集合为0，new一个新子弹加入到集合中
            //并且坦克存活
            if (isALive && shots.size() == 0) {
                //根据Hero对象的位置和方向来创建Shot
                switch (getDirect()) {//获取Hero对象的方向
                    case 0://上
                        shot = new Shot(getX() + 20,getY(),0);
                        break;
                    case 1://右
                        shot = new Shot(getX() + 60,getY() + 20,1);
                        break;
                    case 2://下
                        shot = new Shot(getX() + 20,getY() + 60,2);
                        break;
                    case 3://左
                        shot = new Shot(getX(),getY() + 20,3);
                        break;
                }
                //把创建的Shot对象放入到Shots中
                shots.add(shot);
                //启动shot线程
                new Thread(shot).start();
            }
            //根据方向移动
            switch (getDirect()) {
                //记得多走两步
                case 0://上
                    for (int i = 0; i < 30; i++) {
                        //判断坦克是否超过游戏区域
                        if (getY() > 0 && !isTouchEnemyTank()) {
                            moveUp();
                        }
                        //休眠，防止乱转
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1://右
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000 && !isTouchEnemyTank()) {
                            moveRight();
                        }
                        //休眠，防止乱转
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2://下
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750 && !isTouchEnemyTank()) {
                            moveDown();
                        }
                        //休眠，防止乱转
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3://左
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0 && !isTouchEnemyTank()) {
                            moveLeft();
                        }
                        //休眠，防止乱转
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            //随机改变方向,生成随机数0~3
            setDirect((int) (Math.random() * 4));
            //退出线程
            if (!isALive) break;
        }
    }
}
